package mathgame.ctrl;

import java.util.ArrayList;

import mathgame.core.Chapter;
import mathgame.core.GameState;
import mathgame.core.Highscore;
import mathgame.core.Round;
import mathgame.core.TimeAttackTimer;
import mathgame.core.Chapter.ChapterNames;

import javax.swing.*;

// Control for the Menu in StartPanel Class, will handle all actions on the Menu.

public class Controller {

    private GameState gameState;
    private Round round;
    private TimeAttackTimer timeAttackTimer;
    private ArrayList <Chapter> timeAttackChapters;
    private ArrayList <Chapter> practiceChapters;

    private static Controller controller = null;
    protected Controller() {
        gameState = GameState.getGameState();
        timeAttackTimer = new TimeAttackTimer();
        timeAttackChapters = new ArrayList <Chapter>();
        practiceChapters = new ArrayList <Chapter>();
        initTimeAttackChapters();
    }
    public static Controller getController() {
        if(controller == null) {
            controller = new Controller();
        }
        return controller;
    }

    public void changeCard(int card){
        gameState.changeCard(card);
    }

    public void startPracticeGame(){
        round = new Round(practiceChapters);
        gameState.changeState(round.getProblem(), 0);
    }

    public void startTimeAttackGame(){
        round = new Round(timeAttackChapters);
        gameState.changeState(round.getProblem(), 0);
        gameState.changeState("1 x Multiplier", 1);
        gameState.changeState("Score: 0", 3);
        gameState.changeState("", 2);
    }

    public void newHighscore(String userName){
        Highscore highscore = Highscore.getHighscore();
        highscore.addToHighscore(userName, round.getScore().getPoints());
    }

    public void setAnswer(String answer){

        try{
            round.setAnswer(answer);
            round.correct();
            gameState.changeState(round.getScore().getMultiplier() + "x Multiplier", 1);
            gameState.changeState(round.answerFeedback, 2);
            if (round.correctAns) {
                gameState.changeState("Score: " + round.getScore().getPoints(), 3);
            }
            gameState.changeState(round.getProblem(),0);
        }

        catch ( NumberFormatException badInput ) {
            JOptionPane.showMessageDialog(null, "Please enter only numbers." );
        }
    }

    public void setPracticeAnswer(String answer){
        round.correctTextFile(answer);
        gameState.changeState(round.answerFeedback, 2);
    }

    public void initiateHighscore(){
        Highscore highscore = Highscore.getHighscore();
        highscore.initiateHighscore();
    }

    public Highscore getHighscore(){
        return Highscore.getHighscore();
    }

    public void newTimeAttackTimer(){
        timeAttackTimer = new TimeAttackTimer();
    }
    public TimeAttackTimer getTimeAttackTimer(){
        return timeAttackTimer;
    }

    public GameState getGameState(){
        return GameState.getGameState();
    }

    public void clearPracticeChapters(){
        practiceChapters.clear();
    }
    public void initTimeAttackChapters() {
        timeAttackChapters.add(new Chapter(ChapterNames.ADDITION, " + ", 11, 88 ));
        timeAttackChapters.add(new Chapter(ChapterNames.SUBTRACTION, " - ", 11, 88 ));
        timeAttackChapters.add(new Chapter(ChapterNames.DIVISION, " / ", 2, 79 ));
        timeAttackChapters.add(new Chapter(ChapterNames.MULTIPLICATION, " * ", 2, 10 ));
    }

    public void activateAdditionChapter() {
        practiceChapters.add(new Chapter(ChapterNames.ADDITION, " + ", 11, 88 ));
    }

    public void activateSubtractionChapter() {
        practiceChapters.add(new Chapter(ChapterNames.SUBTRACTION, " - ", 11, 88 ));
    }

    public void activateDivisionChapter() {
        practiceChapters.add(new Chapter(ChapterNames.DIVISION, " / ", 2, 79 ));
    }

    public void activateMultiplicationChapter() {
        practiceChapters.add(new Chapter(ChapterNames.MULTIPLICATION, " * ", 2, 10 ));
    }
    public void activateEquationChapter(){
        practiceChapters.add(new Chapter(ChapterNames.EQUATION, null, 0, 0));
    }
    public void activatePercentChapter(){
        practiceChapters.add(new Chapter(ChapterNames.PERCENT, " % ", 0, 100));
    }

    public Boolean empty(){
        if(practiceChapters.isEmpty()){
            return true;
        }
        else{
            return false;
        }
    }

    public void newProblem(){
        gameState.changeState(round.getProblem(), 0);
    }

    public void clearProblemLbl(){
        gameState.changeState("", 0);
    }

}
